from sklearn.ensemble import RandomForestRegressor

# 创建随机森林模型，并获取特征的重要性
rf = RandomForestRegressor(n_estimators=100, random_state=42).fit(X, y)
importances = rf.feature_importances_

# 特征名称
feature_names = df_encoded.drop('Catch_Rate(kg/hour)', axis=1).columns
importances = rf.feature_importances_
feature_names = df_encoded.drop('Catch_Rate(kg/hour)', axis=1).columns

# 设置图片清晰度
plt.rcParams['figure.dpi'] = 300

# 显示负号
plt.rcParams['axes.unicode_minus'] = False

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['WenQuanYi Zen Hei']

# 画出柱状图，对特征重要性进行可视化
plt.figure(figsize=(10, 8))
plt.bar(feature_names, importances)
plt.xlabel('特征')
plt.xticks(rotation=90)
plt.ylabel('重要性')
plt.title('特征重要性柱状图')
plt.show()


# 模型性能对比图
labels = ['MSE', 'RMSE']
train_scores = [mse, rmse_train]
test_scores = [mean_squared_error(y_test, y_test_pred), np.sqrt(mean_squared_error(y_test, y_test_pred))]

x = range(len(labels))
width = 0.35

plt.bar(x, train_scores, width, color='blue', label='训练集')
plt.bar([i + width for i in x], test_scores, width, color='red', label='测试集')

plt.xlabel('指标')
# 设置X轴刻度标签及位置
plt.xticks([i + width / 2 for i in x], labels)

plt.ylabel('结果')
plt.title('模型性能对比')

# 显示图例
plt.legend()
plt.show()